/*******************************************************************************
 * Package: com.ruoyi.flowable.web
 * Type:    ModelWeb
 * Date:    2023-11-27 20:52
 *
 * Copyright (c) 2023 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.ruoyi.flowable.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.flowable.dto.FlowModelDTO;
import com.ruoyi.flowable.dto.ModelPageReqDTO;
import com.ruoyi.flowable.dto.ModelUpdateDTO;
import com.ruoyi.flowable.service.ModelFlowService;
import com.ruoyi.flowable.vo.ModelUpdateStateVO;
import com.ruoyi.flowable.vo.ModelVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.flowable.ui.modeler.model.ModelRepresentation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.List;

/**
 * 功能描述：流程模型
 *
 * @author Songxianyang
 * @date 2023-11-27 20:52
 */
@RequiredArgsConstructor
@Tag(name = "流程模型")
@RestController
@RequestMapping("/flow/model")
public class ModelWeb {

    private final ModelFlowService modelFlowService;

    @PostMapping("/page")
    @Operation(summary = "列表分页")
    public List<ModelVO> getModelPage(@RequestBody ModelPageReqDTO dto) {
        return modelFlowService.getModelPage(dto);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模型")
    public ModelVO getModel(@RequestParam("id") String id) {
        ModelVO model = modelFlowService.getModel(id);
        return model;
    }

    @PostMapping("/update-state")
    @Operation(summary = "修改模型部署的流程定义状态")
    public String updateModelState(@RequestBody ModelUpdateStateVO vo) {
        return modelFlowService.updateModelState(vo.getId(), vo.getState());
    }

    @DeleteMapping("/delete")
    @Operation(summary =  "删除模型")
    public Boolean deleteModel(@RequestParam("id") String id) {
        return modelFlowService.deleteModel(id);
    }


    /**
     * 添加流程模型
     *
     * @param req
     */

    @PostMapping("/model-insert")
    @Operation(summary =  "添加流程模型")
    public ModelRepresentation modelInsert(@RequestBody FlowModelDTO req){
        return modelFlowService.modelInsert(req);
    }
    /**
     * 导出流程模型的XML
     * @param id 模型id
     * @param response  响应
     */
    @GetMapping("/export-model-xml")
    @Operation(summary =  "导出流程模型的XML")
    @Parameters({
            @Parameter(name = "id", description = "模型id")
    })
    public void exportModelXML(String id, HttpServletResponse response) throws IOException{
        modelFlowService.exportModelXML(id, response);
    }

    /**
     * 修改流程模型
     * @param dto
     */
    @PostMapping("/model-update")
    @Operation(summary =  "修改流程模型")
    public Boolean updateModel(@RequestBody  ModelUpdateDTO dto) throws JsonProcessingException{
       return modelFlowService.updateModel(dto);
    }

    @PostMapping("/import-model")
    @Operation(summary =  "导入模型")
    public Boolean importModel(@RequestParam("file") MultipartFile file) throws JsonProcessingException{
        return modelFlowService.importModel(file);
    }

    /**
     * 部署模型模型
     * @param id
     * @return
     */
    @PostMapping("/deploy")
    @Operation(summary =  "部署模型模型")
    @Parameters({
            @Parameter(name = "id", description = "模型id")
    })
    public Boolean deploy(@RequestParam("id") String id){
        return modelFlowService.deploy(id);
    }

}
